package com.apes.fn.scm.internal.help.service;

import com.apes.fn.scm.internal.help.model.InternalHelpReturnApproval;
import com.apes.fn.scm.internal.help.service.voucher.ReturnAccountService;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * 这里核心处理账目逻辑为：
 * <ul>
 *     <li>基地退回对门店的销售</li>
 *     <li>门店返厂给基地</li>
 * </ul>
 */
@Service
public class InternalHelpFlowVoucherService extends DomainService {

    @Autowired
    private ReturnAccountService returnAccountService;

    /**
     * 基地分销退回凭证
     * <p> 基地退回对门店的销售 </p>
     *
     * @param request InternalHelpReturnApproval
     */
    private void flowSaleReturnVoucher(SimpleRequest request) {
        InternalHelpReturnApproval internalHelpReturnApproval = request.getO(InternalHelpReturnApproval.class);
        returnAccountService.returnInnerHelp(internalHelpReturnApproval);
    }

    /**
     * 门店的返厂凭证
     * <p> 门店返厂给基地 </p>
     *
     * @param request InternalHelpReturnApproval
     */
    public void flowPurchaseReturnVoucher(SimpleRequest request) {
        InternalHelpReturnApproval internalHelpReturnApproval = request.getO(InternalHelpReturnApproval.class);
        Optional.ofNullable(internalHelpReturnApproval).orElseThrow(() -> new RuntimeException("內协退货单不存在"));
        returnAccountService.returnStoreInnerHelp(internalHelpReturnApproval);
    }

}
